﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;

namespace U_Team.WC
{
    public partial class MainWindow : Window
    {
        OpenFileDialog ofd;

        public MainWindow()
        {
            InitializeComponent();
            ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "TXT files|*.txt|DOC files|*.doc|DOCX files|*.docx";
        }

        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            if (ofd.ShowDialog().HasValue)
            {
                var fs = new FileStream(ofd.FileName, FileMode.Open);
                var sr = new StreamReader(fs, Encoding.GetEncoding(1251));
                tBox.Text = sr.ReadToEnd();
            }
        }

        private void CountButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(ProcessInput(tBox.Text) + " words in the text");
        }

        private int ProcessInput(string input)
        {
            char[] separators = {' ', ',', ';', ':', '\n', '\t'};
            string[] wordArray = input.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            return wordArray.Length;
        }
    }
}
